package com.cesare.service.Impl;

import com.cesare.exception.NotFoundException;
import com.cesare.mapper.BlogMapper;
import com.cesare.pojo.Blog;
import com.cesare.pojo.Tag;
import com.cesare.service.BlogService;
import com.cesare.service.TagService;
import com.cesare.service.TypeService;
import com.cesare.utils.MarkdownUtils;
import com.cesare.vo.BlogQuery;
import com.cesare.vo.SearchBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private TagService tagService;
    @Autowired
    private TypeService typeService;

    @Override
    public List<Blog> getAllBLog() {
        return blogMapper.getAllBlog();
    }

    @Transactional
    @Override
    public void addBlog(Blog blog) {
        if(blog.getId()==null){
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
            blogMapper.addBlog(blog);
        }else {
            blog.setUpdateTime(new Date());
        }
    }

    @Transactional
    @Override
    public void updateBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        blogMapper.updateBlog(blog);
    }

    @Override
    public Blog getBlogById(Long id) {
        return blogMapper.getBlogById(id);
    }

    @Override
    public void deleteBlog(Long id) {
        blogMapper.deleteBlog(id);
    }

    @Override
    public List<Blog> searchBlog(Blog blog) {
        return blogMapper.searchBlog(blog);
    }

    @Override
    public List<BlogQuery> getBlogBySearch(SearchBlog searchBlog) {
        return blogMapper.searchByTitleOrTypeOrRecommend(searchBlog);
    }

    @Override
    public List<Blog> getBlogByTypeId(Long id) {
        return blogMapper.getBlogByTypeId(id);
    }

    @Override
    public List<Blog> getBlogByTagId(Long id) {
        return blogMapper.getBlogByTagId(id);
    }

    @Override
    public List<Blog> getIndexBlog() {
        return blogMapper.getIndexBlog();
    }

    @Override
    public List<Blog> getRecommendBlog() {
        return blogMapper.getRecommendBlog();
    }

    @Override
    public Blog getDetailedBlog(Long id) {
        Blog blog = blogMapper.getDetailedBlog(id);
        if(blog==null){
            throw new NotFoundException("这个博客不存在");
        }
        List<Tag> tags = new ArrayList<>();
        if (blog.getTagIds()=="" || blog.getTags().equals("")){

        }else {
            String[] tagIds = blog.getTagIds().split(",");
            for(String tagId:tagIds){
                Long tagid = Long.parseLong(tagId);
                tags.add(tagService.getTagById(tagid));
            }
            blog.setType(typeService.getTypeById(blog.getTypeId()));
            blog.setTags(tags);
        }
        String content = blog.getContent();
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        blogMapper.updateViews(id);
        return blog;
    }

    @Override
    public List<Blog> searchIndexBlog(String query) {
        return blogMapper.searchIndexBlog(query);
    }

    @Override
    public Integer countBlog() {
        return blogMapper.countBlog();
    }

    @Override
    public Map<String, List<Blog>> archiveBlog() {
        List<String> years=blogMapper.findGroupYear();
        Map<String, List<Blog>> map=new HashMap<>();
        for(String year:years){
            map.put(year,blogMapper.findByYear(year));
        }
        return map;
    }
}
